package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Image;
import com.ws.lv.service.ImageService;
import com.ws.lv.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.UUID;

@Controller
@RequestMapping("/image")
public class ImageController {

    @Autowired
    ImageService imageService;

    // 跳转到添加附图片页面
    @RequestMapping("/toInsertImage")
    public String toInsertImage(int routeId, Model model) {
        System.out.println("跳转到添加附图片页面");
        model.addAttribute("routeId", routeId);
        return "adminpage/image/add";
    }

    // 添加附图片
    @RequestMapping("/insertImage")
    public String insertImage(Image image, ArrayList<MultipartFile> fuTu) {
        System.out.println("添加附图片");
        System.out.println(image);
        try {
            for (MultipartFile file : fuTu){
                System.out.println(file.getOriginalFilename());
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String suffix = file.getOriginalFilename().split("\\.")[1];
                image.setImage_url(uuid+"."+suffix);
                int i = imageService.insertImage(image);
                file.transferTo(new File(Res.fuTuFileDir+uuid+"."+suffix));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/route/selectRoute";
    }

    // 查询附图片
    @RequestMapping("/selectImages")
    public String selectImages(String route_name, Integer image_state, Model model , Integer currentPage) {
        System.out.println("查询附图片");
        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null){
            pageHelper.startPage(1, 5);
        }else {
            pageHelper.startPage(currentPage, 5);
        }
        ArrayList<Image> images = (ArrayList<Image>) imageService.selectImages(route_name, image_state);
        PageInfo pageInfo = new PageInfo(images);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("route_name", route_name);
        model.addAttribute("image_state", image_state);

        return "adminpage/image/list";
    }

    //修改状态为显示
    @RequestMapping("/updateImageDisplay")
    public String updateImageDisplay(int image_id) {
        System.out.println("修改状态为显示");
        int i = imageService.updateImageDisplay(image_id);
        return "forward:/image/selectImages";
    }
    //修改状态为隐藏
    @RequestMapping("/updateImageHide")
    public String updateImageHide(int image_id) {
        System.out.println("修改状态为隐藏");
        int i = imageService.updateImageHide(image_id);
        return "forward:/image/selectImages";
    }

    // 批量删除附图
    @RequestMapping("/deleteImages")
    public String deleteImages(String imageIds) {
        System.out.println("批量删除附图");
        String[] split = imageIds.split(",");
        ArrayList<Integer> ids = new ArrayList<>();
        if (imageIds != null && imageIds.trim().length() > 0){
            for (String s : split){
                ids.add(Integer.parseInt(s));
            }
            for (Integer id : ids){
                Image image = imageService.selectImageById(id);
                File file = new File(Res.fuTuFileDir+image.getImage_url());
                if (file.exists()){
                    file.delete();
                }
            }
            int i = imageService.deleteImages(ids);
        }
        return "redirect:/image/selectImages";
    }

    //去修改附图页面
    @RequestMapping("/toUpdateImage")
    public String toUpdateImage(int image_id, Model model) {
        System.out.println("去修改附图页面");
        Image image = imageService.selectImageById(image_id);
        model.addAttribute("image", image);
        return "adminpage/image/update";
    }

    //修改附图
    @RequestMapping("/updateImage")
    public String updateImage(Image image, MultipartFile fuTu) {
        System.out.println("修改附图");
        Image oldImage = imageService.selectImageById(image.getImage_id());
        try {
            if (fuTu.getOriginalFilename().length() > 0){
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String suffix = fuTu.getOriginalFilename().split("\\.")[1];
                image.setImage_url(uuid+"."+suffix);
                File file = new File(Res.fuTuFileDir+oldImage.getImage_url());
                if (file.exists()){
                    file.delete();
                }
                int i = imageService.updateImage(image);
                fuTu.transferTo(new File(Res.fuTuFileDir+uuid+"."+suffix));

            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/image/selectImages";

    }







}
